package com.audi.box.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.audi.dto.DeviceItem;
import com.audi.dto.HistoryItem;
import com.audi.dto.MemberItem;
import com.audi.dto.PlaceItem;
import com.audi.dto.SceneDeviceItem;
import com.audi.dto.SceneItem;
import com.audi.dto.SmartTriggerWhenItem;

public class SQLiteAsusHelperCommon extends SQLiteAsusHelper {
    
    public SQLiteAsusHelperCommon(Context context) {
        super(context);
    }
    
    /**
     * Convert DeviceItem to ContentValues for Add and Update function
     * 
     * @param device
     *            DeviceItem
     * @return ContentValues
     */
    protected ContentValues convertDeviceItemToContentValue(final DeviceItem device) {
        ContentValues values = new ContentValues();
        
        if (device.getDeviceId() != null) {
            values.put(KEY_DEVICE_ID, device.getDeviceId());
        }
        
        if (device.getDeviceName() != null) {
            values.put(KEY_DEVICE_NAME, device.getDeviceName());
        }
        
        if (device.getDeviceIcon() != null) {
            values.put(KEY_DEVICE_ICON, device.getDeviceIcon());
        }
        
        if (device.getDeviceStatus() != null) {
            values.put(KEY_DEVICE_STATUS, device.getDeviceStatus());
        }
        
        if (device.getDeviceType() != null) {
            values.put(KEY_DEVICE_TYPE, device.getDeviceType());
        }
        
        if (device.getStatusValue() != null) {
            values.put(KEY_DEVICE_CONTROL_VALUE, device.getStatusValue());
        }
        
        if (device.getDeviceCode() != null) {
            values.put(KEY_DEVICE_CODE, device.getDeviceCode());
        }
        
        if (device.getDeviceBoxId() != null) {
            values.put(KEY_DEVICE_BOX_ID, device.getDeviceBoxId());
        }
        
        if (device.getDevicePlaceId() != null) {
            values.put(KEY_DEVICE_PLACE_ID, device.getDevicePlaceId());
        }
        
        if (device.getUserId() != null) {
            values.put(KEY_DEVICE_USER_ID, device.getUserId());
        }
        
        if (device.getFavorite() != null) {
            values.put(KEY_DEVICE_FAVORITE, device.getFavorite());
        }
        
        if (device.getIsDeleted() != null) {
            values.put(KEY_DEVICE_IS_DELETED, device.getIsDeleted());
        }
        
        return values;
    }
    
    /**
     * Create DeviceItem from Database cursor
     * 
     * @param cursor
     *            final Cursor
     * @return DeviceItem
     */
    protected DeviceItem createDeviceItem(final Cursor cursor) {
        
        int columnIndex = 0;
        DeviceItem deviceItem = new DeviceItem();
        
        deviceItem.setDeviceId(cursor.getInt(columnIndex));
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_NAME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceName(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_ICON);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceIcon(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_STATUS);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceStatus(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_TYPE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceType(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_CONTROL_VALUE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setStatusValue(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_CODE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceCode(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_BOX_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDeviceBoxId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_PLACE_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setDevicePlaceId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_USER_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            
            deviceItem.setUserId(cursor.getInt(columnIndex));
            deviceItem.setShowUserStatus(1);
            
            columnIndex = cursor.getColumnIndex(KEY_USER_ICON);
            if (columnIndex != -1 && cursor.isNull(columnIndex)) {
                deviceItem.setUserIcon(cursor.getString(columnIndex));
            }
            
            columnIndex = cursor.getColumnIndex(KEY_USER_STATUS);
            if (columnIndex != -1 && cursor.isNull(columnIndex)) {
                deviceItem.setUserStatus(cursor.getInt(columnIndex));
            }
            
        } else {
            deviceItem.setUserId(0);
            deviceItem.setShowUserStatus(0);
            deviceItem.setUserIcon("");
            deviceItem.setUserStatus(0);
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_FAVORITE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setFavorite(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_DEVICE_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            deviceItem.setIsDeleted(cursor.getInt(columnIndex));
        }
        
        return deviceItem;
    }
    
    /**
     * Convert PlaceItem to ContentValues for Add and Update function
     * 
     * @param place
     *            PlaceItem
     * @return ContentValues
     */
    protected ContentValues convertPlaceItemToContentValue(final PlaceItem place) {
        ContentValues values = new ContentValues();
        
        if (place.getPlaceId() != null) {
            values.put(KEY_PLACE_ID, place.getPlaceId());
        }
        
        if (place.getPlaceName() != null) {
            values.put(KEY_PLACE_NAME, place.getPlaceName());
        }
        
        if (place.getPlaceImage() != null) {
            values.put(KEY_PLACE_ICON, place.getPlaceImage());
        }
        
        if (place.getCapabilityType() != null) {
            values.put(KEY_PLACE_CAPABILITY_TYPE, place.getCapabilityType());
        }
        
        if (place.getIsDeleted() != null) {
            values.put(KEY_PLACE_IS_DELETED, place.getIsDeleted());
        }
        
        return values;
    }
    
    /**
     * Create PlaceItem from Database cursor
     * 
     * @param cursor
     *            final Cursor
     * @return PlaceItem
     */
    protected PlaceItem createPlaceItem(final Cursor cursor) {
        int columnIndex = 0;
        PlaceItem placeItem = new PlaceItem();
        
        columnIndex = cursor.getColumnIndex(KEY_PLACE_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            placeItem.setPlaceId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_PLACE_NAME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            placeItem.setPlaceName(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_PLACE_ICON);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            placeItem.setPlaceImage(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_PLACE_CAPABILITY_TYPE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            placeItem.setCapabilityType(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_PLACE_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            placeItem.setIsDeleted(cursor.getInt(columnIndex));
        }
        
        return placeItem;
    }
    
    /**
     * Convert SceneItem to ContentValues for Add/Update function
     * 
     * @param scene
     *            SceneItem
     * @return ContentValues
     */
    protected ContentValues convertSceneItemToContentValue(final SceneItem scene) {
        ContentValues values = new ContentValues();
        
        if (scene.getSceneId() != null) {
            values.put(KEY_SCENE_ID, scene.getSceneId());
        }
        
        if (scene.getSceneTitle() != null) {
            values.put(KEY_SCENE_NAME, scene.getSceneTitle());
        }
        
        if (scene.getSceneIcon() != null) {
            values.put(KEY_SCENE_ICON, scene.getSceneIcon());
        }
        
        if (scene.getSceneBackground() != null) {
            values.put(KEY_SCENE_COLOR, scene.getSceneBackground());
        }
        
        if (scene.getPlaceId() != null) {
            values.put(KEY_SCENE_PLACE_ID, scene.getPlaceId());
        }
        
        if (scene.getIsDeleted() != null) {
            values.put(KEY_SCENE_IS_DELETED, scene.getIsDeleted());
        }
        
        return values;
    }
    
    /**
     * Create SceneItem from final Cursor
     * 
     * @param cursor
     *            final Cursor
     * @return SceneItem
     */
    protected SceneItem createSceneItem(final Cursor cursor) {
        
        int columnIndex = 0;
        SceneItem sceneItem = new SceneItem();
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setSceneId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_NAME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setSceneTitle(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_ICON);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setSceneIcon(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_PLACE_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setPlaceId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_COLOR);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setSceneBackground(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneItem.setIsDeleted(cursor.getInt(columnIndex));
        }
        
        return sceneItem;
    }
    
    /**
     * Convert SceneDeviceItem to ContentValues for Add/Update SceneDevice
     * function.
     * 
     * @param sceneDevice
     *            SceneDeviceItem
     * @return ContentValues
     */
    protected ContentValues convertSceneDeviceItemToContentValue(final SceneDeviceItem sceneDevice) {
        ContentValues values = new ContentValues();
        
        if (sceneDevice.getScene().getSceneId() != null) {
            values.put(KEY_SCENE_DEVICE_SCENE_ID, sceneDevice.getScene().getSceneId());
        }
        
        if (sceneDevice.getDevice().getDeviceId() != null) {
            values.put(KEY_SCENE_DEVICE_DEVICE_ID, sceneDevice.getDevice().getDeviceId());
        }
        
        if (sceneDevice.getScenceDeviceStatus() != null) {
            values.put(KEY_SCENE_DEVICE_DEVICE_STATUS, sceneDevice.getScenceDeviceStatus());
        }
        
        if (sceneDevice.getIsDeleted() != null) {
            values.put(KEY_SCENE_IS_DELETED, sceneDevice.getIsDeleted());
        }
        
        return values;
    }
    
    /**
     * Create SceneDeviceItem from Database cursor
     * 
     * @param cursor
     *            final Cursor
     * @return SceneDeviceItem
     */
    protected SceneDeviceItem createSceneDeviceItem(final Cursor cursor) {
        SceneDeviceItem sceneDevice = new SceneDeviceItem();
        
        int columnIndex = 0;
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_DEVICE_DEVICE_STATUS);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneDevice.setScenceDeviceStatus(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_SCENE_DEVICE_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            sceneDevice.setIsDeleted(cursor.getInt(columnIndex));
        }
        
        return sceneDevice;
    }
    
    /**
     * Convert MemberItem to ContentValues for Add and Update function
     * 
     * @param member
     *            MemberItem
     * @return ContentValues
     */
    protected ContentValues convertMemberItemToContentValue(final MemberItem member) {
        ContentValues values = new ContentValues();
        
        if (member.getMemberId() != null) {
            values.put(KEY_USER_ID, member.getMemberId());
        }
        
        if (member.getMemberName() != null) {
            values.put(KEY_USER_NAME, member.getMemberName());
        }
        
        if (member.getMemberEmail() != null) {
            values.put(KEY_USER_EMAIL, member.getMemberEmail());
        }
        
        if (member.getMemberPhoto() != null) {
            values.put(KEY_USER_ICON, member.getMemberPhoto());
        }
        
        if (member.getMemberStatus() != null) {
            values.put(KEY_USER_STATUS, member.getMemberStatus());
        }
        
        if (member.getMemberDeleted() != null) {
            values.put(KEY_USER_IS_DELETED, member.getMemberDeleted());
        }
        
        return values;
    }
    
    /**
     * Create MemberItem from final Cursor
     * 
     * @param cursor
     *            final Cursor
     * @return MemberItem
     */
    protected MemberItem createMemberItem(final Cursor cursor) {
        int columnIndex = 0;
        MemberItem memberItem = new MemberItem();
        
        columnIndex = cursor.getColumnIndex(KEY_USER_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_USER_NAME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberName(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_USER_EMAIL);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberEmail(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_USER_ICON);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberPhoto(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_USER_STATUS);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberStatus(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_USER_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            memberItem.setMemberDeleted(cursor.getInt(columnIndex));
        }
        
        return memberItem;
    }
    
    /**
     * Create SmartTriggerWhenItem from Database cursor
     * 
     * @param cursor
     *            final Cursor
     * @return SmartTriggerWhenItem
     */
    protected SmartTriggerWhenItem createSTWhenItem(final Cursor cursor) {
        SmartTriggerWhenItem stWhen = new SmartTriggerWhenItem();
        int columnIndex = 0;
        
        columnIndex = cursor.getColumnIndex(KEY_ST_WHEN_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            stWhen.setWhenId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_ST_WHEN_DEVICE_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            stWhen.setWhenDeviceId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_ST_WHEN_VALUE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            stWhen.setWhenDeviceValues(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_ST_WHEN_TYPE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            stWhen.setWhenDeviceType(cursor.getInt(columnIndex));
        }
        
        /*
         * CHUA XONG, CAN LAM THEM
         */
        columnIndex = cursor.getColumnIndex(KEY_ST_WHEN_IS_DELETED);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            // stWhen.set(cursor.getInt(columnIndex));
        }
        
        return stWhen;
    }
    
    /*
     * CHUA XONG, CAN LAM THEM
     */
    /**
     * Convert SmartTriggerWhenItem to ContentValues for Add and Update function
     * 
     * @param When
     *            SmartTriggerWhenItem
     * @return ContentValues
     */
    protected ContentValues convertSTWhenItemToContentValue(final SmartTriggerWhenItem stWhen) {
        ContentValues values = new ContentValues();
        
        /*
         * if (member.getMemberName() != null) { values.put(KEY_USER_NAME,
         * member.getMemberName()); }
         */
        
        return values;
    }
    
    /**
     * Convert MemberItem to ContentValues for Add and Update function
     * 
     * @param member
     *            MemberItem
     * @return ContentValues
     */
    protected ContentValues convertHistoryItemToContentValue(final HistoryItem history) {
        ContentValues values = new ContentValues();
        
        if (history.getHistoryId() != null) {
            values.put(KEY_HISTORY_ID, history.getHistoryId());
        }
        
        if (history.getTarrgetId() != null) {
            values.put(KEY_HISTORY_TARGET_ID, history.getTarrgetId());
        }
        
        if (history.getTarrgetName() != null) {
            values.put(KEY_HISTORY_TARGET_NAME, history.getTarrgetName());
        }
        
        if (history.getTarrgetIcon() != null) {
            values.put(KEY_HISTORY_TARGET_ICON, history.getTarrgetIcon());
        }
        
        if (history.getTarrgetType() != null) {
            values.put(KEY_HISTORY_TARGET_TYPE, history.getTarrgetType());
        }
        
        if (history.getTarrgetType() != null) {
            values.put(KEY_HISTORY_TARGET_TYPE, history.getTarrgetType());
        }
        
        if (history.getTarrgetAction() != null) {
            values.put(KEY_HISTORY_TARGET_ACTION, history.getTarrgetAction());
        }
        
        if (history.getTarrgetSource() != null) {
            values.put(KEY_HISTORY_TARGET_SOURCE, history.getTarrgetSource());
        }
        
        if (history.getTarrgetDestination() != null) {
            values.put(KEY_HISTORY_TARGET_DESTINATION, history.getTarrgetDestination());
        }
        
        if (history.getDescription() != null) {
            values.put(KEY_HISTORY_DESCRIPTION, history.getDescription());
        }
        
        if (history.getUserId() != null) {
            values.put(KEY_HISTORY_USER, history.getUserId());
        }
        
        Log.e("SQLite Helper Common convertHistoryItemToContentValue", "" + history.getDateTime());
        if (history.getDateTime() != null) {
            values.put(KEY_HISTORY_DATETIME, history.getDateTime());
        }
        
        Log.e("SQLite Helper Common convertHistoryItemToContentValue",
                "" + values.getAsLong(KEY_HISTORY_DATETIME));
        return values;
    }
    
    /**
     * Create HistoryItem from final Cursor
     * 
     * @param cursor
     *            final Cursor
     * @return HistoryItem
     */
    protected HistoryItem createHistoryItem(final Cursor cursor) {
        int columnIndex = 0;
        HistoryItem historyItem = new HistoryItem();
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setHistoryId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_ID);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_NAME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetName(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_ICON);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetIcon(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_TYPE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetType(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_ACTION);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetAction(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_SOURCE);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetSource(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_TARGET_DESTINATION);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setTarrgetDestination(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_DESCRIPTION);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setDescription(cursor.getString(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_USER);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setUserId(cursor.getInt(columnIndex));
        }
        
        columnIndex = cursor.getColumnIndex(KEY_HISTORY_DATETIME);
        if (columnIndex != -1 && !cursor.isNull(columnIndex)) {
            historyItem.setDateTime(cursor.getLong(columnIndex));
        }
        
        return historyItem;
    }
    
}
